using LicenseApp.Client;

namespace LicenseApp.Test;

/// <summary>
/// 机器码检查工具
/// </summary>
public static class MachineCodeChecker
{
    /// <summary>
    /// 检查当前机器码
    /// </summary>
    public static void CheckCurrentMachineCode()
    {
        Console.WriteLine("=== 机器码检查 ===");
        Console.WriteLine();

        try
        {
            var currentMachineCode = MachineCodeGenerator.GenerateMachineCode();
            Console.WriteLine($"当前机器码: {currentMachineCode}");
            Console.WriteLine();

            // 检查授权码中的机器码
            var expectedMachineCode = "QY6Z-TSSC-3BK2-1ZFA";
            Console.WriteLine($"授权码中的机器码: {expectedMachineCode}");
            Console.WriteLine();

            if (currentMachineCode == expectedMachineCode)
            {
                Console.WriteLine("✅ 机器码匹配！");
            }
            else
            {
                Console.WriteLine("❌ 机器码不匹配！");
                Console.WriteLine();
                Console.WriteLine("解决方案:");
                Console.WriteLine("1. 使用生产工具重新生成授权码，使用当前机器码");
                Console.WriteLine("2. 或者生成不绑定机器码的授权码（机器码留空）");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"❌ 检查机器码时发生错误: {ex.Message}");
        }

        Console.WriteLine();
        Console.WriteLine("=== 检查完成 ===");
    }

    /// <summary>
    /// 测试授权码验证
    /// </summary>
    public static void TestLicenseValidation()
    {
        Console.WriteLine("=== 授权码验证测试 ===");
        Console.WriteLine();

        var licenseCode = "eyJTdGFydERhdGUiOiIyMDI1LTA3LTE2IiwiRGF5cyI6MzAsIkFndkNvdW50IjoxLCJNYWNoaW5lQ29kZSI6IlFZNlotVFNTQy0zQksyLTFaRkEiLCJTaWduYXR1cmUiOiJVTW1ta0JjXHUwMDJCVGgwaWdteDJBdEEvcEZScFljTXh2WmhFSy8xRVF4SUNocDh3L2ZlN1lUeTBwVG1LeFlpZVVZd25JeDdycnpwVnVMYmZKb1d6RVhLSHFONE4veWRIa1ZERGRuall1Y1lEYTZvRm1rUlZkLzZqTy9sWTJLZVpGc3B5UnhrODNPckRaVy9IejBwaEpMSERpQlUzWW90dTJ5V1FBdUlMTERjYms0M2RoaUVNd0I5T2Z3dlRZWTVNbWF0VE5jaWlrSU9XVzROQmZTRkVwVjNwcm9YbTlDWHNIaHg1Q3dmMFJ1N0tBUTlVL01vSEVsU1hXNDhcdTAwMkJGTmllbXF3cGZCQ0x4Q2s2Y2U3SDFXZnpHTlNxOEJCVDVjXHUwMDJCOWZBczk2YTR4ZmcyTGljdXcvV0puWnh1ZE9TWjgyR3ZXYk5nR1x1MDAyQnV5SEFlUlgwZUNSckxBRW1rNm04QT09In0=";

        try
        {
            Console.WriteLine("1. 检查公钥文件:");
            
            // 获取应用程序路径
            string assemblyFilePath = System.Diagnostics.Process.GetCurrentProcess().MainModule?.FileName ?? "";
            string assemblyDirPath = System.IO.Path.GetDirectoryName(assemblyFilePath) ?? "";
            string publicKeyPath = System.IO.Path.Combine(assemblyDirPath, "public.xml");

            if (System.IO.File.Exists(publicKeyPath))
            {
                Console.WriteLine($"   ✅ 公钥文件存在: {publicKeyPath}");
                var publicKeyContent = System.IO.File.ReadAllText(publicKeyPath);
                Console.WriteLine($"   公钥内容长度: {publicKeyContent.Length} 字符");
            }
            else
            {
                Console.WriteLine($"   ❌ 公钥文件不存在: {publicKeyPath}");
                return;
            }

            Console.WriteLine();
            Console.WriteLine("2. 检查机器码:");
            var currentMachineCode = MachineCodeGenerator.GenerateMachineCode();
            Console.WriteLine($"   当前机器码: {currentMachineCode}");

            Console.WriteLine();
            Console.WriteLine("3. 尝试注册授权码:");
            
            // 初始化授权系统
            var publicKeyContent2 = System.IO.File.ReadAllText(publicKeyPath);
            var initResult = LicenseManager.Initialize(publicKeyContent2);
            Console.WriteLine($"   初始化结果: {(initResult ? "成功" : "失败")}");

            if (initResult)
            {
                var registerResult = LicenseManager.RegisterLicense(licenseCode);
                Console.WriteLine($"   注册结果: {(registerResult ? "成功" : "失败")}");

                if (registerResult)
                {
                    var licenseInfo = LicenseManager.GetLicenseInfo();
                    if (licenseInfo != null)
                    {
                        Console.WriteLine($"   ✅ 授权信息获取成功:");
                        Console.WriteLine($"      生效日期: {licenseInfo.StartDate:yyyy-MM-dd}");
                        Console.WriteLine($"      授权天数: {licenseInfo.Days}");
                        Console.WriteLine($"      AGV数量: {licenseInfo.AgvCount}");
                        Console.WriteLine($"      剩余天数: {licenseInfo.RemainingDays}");
                    }
                    else
                    {
                        Console.WriteLine($"   ❌ 无法获取授权信息");
                    }
                }
                else
                {
                    Console.WriteLine($"   ❌ 注册失败，可能原因:");
                    Console.WriteLine($"      1. 机器码不匹配");
                    Console.WriteLine($"      2. 公钥不匹配");
                    Console.WriteLine($"      3. 授权码格式错误");
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"❌ 测试过程中发生错误: {ex.Message}");
        }

        Console.WriteLine();
        Console.WriteLine("=== 测试完成 ===");
    }
}
